home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Development Kits / HyperCard Related / APDA HyperCard Toolkits / HyperCard Serial Toolkit 2.6 / Source Code / recvBytes.p < prev    next >
Encoding:
Text File  |  1995-02-07  |  2.7 KB  |  114 lines  |  [TEXT/MPS ]

  1. (*
  2.     recvBytes(count) -- Return count bytes from the serial port, in ASCII, with one byte per item in the
  3.         returned string.
  4.  
  5.     To compile and link this file using Macintosh Programmer's Workshop,
  6.  
  7.         pascal -w recvBytes.p
  8.         link -m ENTRYPOINT -o HyperCommands -rt XFCN=7037 -sn Main=recvBytes ∂
  9.             recvBytes.p.o "{MPW}"Libraries:interface.o "{MPW}"Libraries:Libraries:HyperXLib.o
  10.  
  11.     © Copyright 1989 by Apple Computer, Inc.
  12.  
  13.     Initial coding 12/89 by Harry R. Chesley.
  14. *)
  15.  
  16. {$R-}
  17.  
  18. {$S recvBytes }     { Segment name must be the same as the command name. }
  19.  
  20. unit DummyUnit;
  21.  
  22. interface
  23.  
  24. uses MemTypes, QuickDraw, OSIntf, HyperXCmd;
  25.  
  26. procedure EntryPoint(paramPtr: XCmdPtr);
  27.     
  28. implementation
  29.  
  30. procedure recvBytes(paramPtr: XCmdPtr); forward;
  31.  
  32. procedure EntryPoint(paramPtr: XCmdPtr);
  33.  
  34.     begin
  35.         recvBytes(paramPtr);
  36.     end;
  37.  
  38. procedure recvBytes(paramPtr: XCmdPtr);
  39.  
  40.     var readHandle: Handle;
  41.         readCount: longInt;
  42.         readCountStr: Str255;
  43.         l, l2: longInt;
  44.         p: Ptr;
  45.         s: Str255;
  46.  
  47.     procedure Fail(errMsg: Str255); { set theResult and quit }
  48.         begin
  49.             paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
  50.             exit(recvBytes);
  51.         end;
  52.  
  53.     {$I SPortUtil.inc}
  54.  
  55.     begin
  56.         if paramPtr^.paramCount <> 1 then Fail('parameter count is not 1');
  57.  
  58.         ZeroToPas(paramPtr,paramPtr^.params[1]^,readCountStr);        { First parameter is count to read. }
  59.         readCount := StrToNum(paramPtr,readCountStr);
  60.         if readCount < 0 then Fail('invalid count');
  61.  
  62.         SetUpSPortGlobals;
  63.         EnsureOpenPort;
  64.  
  65.         { Create the input handle. }
  66.         readHandle := NewHandle(readCount);
  67.         HLock(readHandle);
  68.         { Read, read, read... }
  69.         if readCount > 0 then
  70.             begin
  71.                 l := readCount;
  72.                 if FSRead(ThisSPort.portInDev,l,readHandle^) <> noErr then
  73.                     begin
  74.                         DisposHandle(readHandle);
  75.                         Fail('FSRead failed');
  76.                     end;
  77.                 { Shrink the handle if we didn't get everything (paranoid programing; this should be impossible). }
  78.                 if l <> readCount then SetHandleSize(readHandle,l);
  79.             end
  80.         else l := 0;
  81.         { Should we strip? }
  82.         if ThisSPort.stripIncoming then
  83.             begin
  84.                 { If so, rip dem bits off. }
  85.                 p := readHandle^;
  86.                 for l2 := 1 to l do
  87.                     begin
  88.                         p^ := BAND(p^,$7F);
  89.                         p := pointer(ord4(p)+1);
  90.                     end;
  91.             end;
  92.         { Convert to a string. }
  93.         paramPtr^.returnValue := NewHandle(0);
  94.         p := readHandle^;
  95.         for l2 := 1 to l do
  96.             begin
  97.                 LongToStr(paramPtr,BAND(p^,$FF),s);
  98.                 s := Concat(s,',');
  99.                 if PtrAndHand(pointer(ord4(@s)+1),paramPtr^.returnValue,length(s)) <> noErr then
  100.                     begin
  101.                         DisposHandle(readHandle);
  102.                         DisposHandle(paramPtr^.returnValue);
  103.                         Fail('PtrAndHand failed');
  104.                     end;
  105.                 p := pointer(ord4(p)+1);
  106.             end;
  107.         p := pointer(ord4(paramPtr^.returnValue^)+GetHandleSize(paramPtr^.returnValue)-1);
  108.         if p <> paramPtr^.returnValue^ then p^ := 0;
  109.         HUnlock(readHandle);
  110.         DisposHandle(readHandle);
  111.     end;
  112.  
  113. end.
  114.